/**
 * 
 * @param {工具} NKQ
 *  
 */
import { getMembers } from '@/api/api'
import { fileUploadFile, downloadFile } from '@/api/api'
import store from '../store/index'

export function formateStatus (cellValue) {
  let statusName = "";
  let statusColor = "";
  if (cellValue == 0) {
    (statusColor = "#888888"), (statusName = "一般");
  }
  if (cellValue == 1) {
    (statusColor = "#FFAE22"), (statusName = "紧急");
  }
  if (cellValue == 2) {
    (statusColor = "#FF2F64"), (statusName = "非常紧急");
  }
  return `<span style="color:${statusColor}">${statusName}</span>`;
}

export function formateStatus2 (val) {
  let statusName = ''
  if (val == 0) {
    statusName = "一般"
  }
  if (val == 1) {
    statusName = "紧急"
  }
  if (val == 2) {
    statusName = "非常紧急"
  }
  return statusName
}

// 节点进行判断
// 实施方案
export function getNode (nodeId) {
  let node = 2
  if (nodeId == 'T10003') {
    node = 3
  }
  if (nodeId == 'T10004') {
    node = 4
  }
  if (nodeId == 'T10005') {
    node = 5
  }
  if (nodeId == 'T10006') {
    node = 6
  }
  if (nodeId == 'T10007') {
    node = 7
  }
  if (nodeId == 'T10008') {
    node = 8
  }
  if (nodeId == 'T10009') {
    node = 9
  }
  if (nodeId == 'T10010') {
    node = 10
  }
  if (nodeId == 'T10011') {
    node = 11
  }
  if (nodeId == 'T10012') {
    node = 12
  }
  if (nodeId == 'T10013') {
    node = 13
  }
  return node
}
// 招投标
export function getNodeBidding (nodeId) {

}

// 下一节点对象（不并行）
export function nextNodeArr (curAuthor, Author, NodeID, NodeName, curNodeID, curNodeName) {
  let authors = ''
  if (Author.length > 0) {
    Author.forEach(item => {
      authors = authors + item + ','
    })
  }
  let nodeTowardsInfo = []
  let nodeObj = {}
  nodeObj.currentAuthor = curAuthor
  nodeObj.nextNodeID = NodeID
  nodeObj.nextAuthor = authors.removePunctuation(',')
  nodeObj.nextNodeName = NodeName
  nodeObj.nextAuthorCN = ''
  nodeObj.currentNodeID = curNodeID
  nodeObj.currentNodeName = curNodeName
  nodeTowardsInfo.push(nodeObj)
  return JSON.stringify(nodeTowardsInfo)
}

// 下拉多选框获取label值
export function getMultipleLabel (val, arrList) {
  let label = ''
  if (val.length > 0 && arrList.length > 0) {
    val.forEach(item => {
      arrList.find(item2 => {
        if (item2.value == item) {
          label = label + item2.label + ','
        }
      })
    })
  }
  return label.removePunctuation(',')
}

// 下拉单选框获取label值
export function getSingleLabel (val, arrList) {

}

// 获取提交人员姓名（不并行）
export function getName (arrList, nodeId) {
  let name = ''
  let arr = JSON.parse(arrList)
  if (arr.length > 0) {
    let obj = {}
    obj = arr.find(item => {
      return item.nextNodeID === nodeId
    })
    name = obj.nextAuthorCN
  }
  return name
}

// 获取提交人员域账号
export function getAccount (arrList, nodeId) {
  let account = ''
  let arr = JSON.parse(arrList)
  if (arr.length > 0) {
    let obj = {}
    obj = arr.find(item => {
      return item.currentNodeID === nodeId
    })
    account = obj.nextAuthor  // 取当前流程的下一处理人
  }
  return account
}
export function getAccountBack (arrList, nodeId) {
  let account = ''
  let arr = JSON.parse(arrList)
  if (arr.length > 0) {
    let obj = {}
    obj = arr.find(item => {
      return item.currentNodeID === nodeId
    })
    account = obj.currentAuthor  // 取当前流程的当前处理人
  }
  return account
}

// 获取nodeTowardsInfo中人员姓名
export function getNodeTowardsInfoName (nodeTowardsInfo) {
  var arr = JSON.parse(nodeTowardsInfo)
  let name = ''
  name = arr[0].nextAuthorCN
  return name
}
// 获取nodeTowardsInfo中人员域账号
export function getNodeTowardsInfoAccount (nodeTowardsInfo) {
  var arr = JSON.parse(nodeTowardsInfo)
  let account = ''
  account = arr[0].nextAuthor
  return account
}

// 上传文件
export async function getFileId (vm, fileList, n1, n2) {
  // console.log(store.state.jobAccount)
  // console.log(fileList)
  // console.log(vm)
  let fileListId = []
  const loading = vm.$loading({
    lock: true,
    text: '文件上传提交中...',
    spinner: 'el-icon-loading',
    background: 'rgba(0, 0, 0, 0.7)'
  })
  if (fileList.length > 0) {
    let formData = new FormData()
    // fileList.forEach(item => {
    //   formData.append('file', item)
    // })

    fileList.forEach(item => {
      // 通过id判断，如果不是通过文件上传来的文件，则不提交
      if (!item.id) {
        formData.append('file', item)
      }
    })
    formData.append('userId', store.state.jobAccount || 'unknow')
    formData.append('businessId', n1)  // n1代表不同方案流
    formData.append('businessType', n2)   // n2代表某个方案流创建某个流程的页面
    await fileUploadFile(formData).then(res => {
      // console.log(res)
      if (res.code == 200) {
        if (res.data.length > 0) {
          res.data.forEach(item => {
            let obj = {}
            obj.id = item.id
            obj.fileName = item.fileName
            fileListId.push(obj)
            loading.close()
          })
        } else {
          loading.close()
        }
      }
    })
  } else {
    loading.close()
  }
  return fileListId
}

// 获取编辑状态下的上传文件;显示在上传组件中
export function editFiles(files) {
  if (files.length) {
    let fileList = []
    let fileList2 = []
    let pArr = []
    files.forEach(item => {
      pArr.push(new Promise((resolve, reject) => {
        downloadFile({ id: item.id }).then(res => {
          let obj = {}
          obj.name = item.fileName
          obj.url = res.data.url
          obj.id = item.id
          obj.fileName = item.fileName
          fileList2.push(obj)
          resolve(obj)
        })
      }))
    })
    Promise.all(pArr).then(res => {
      // console.log(res)
      fileList.push(...res)
    })
    return [fileList, fileList2]
  }
}

// 文件下载
export function downfile (id) {
  let params = {
    id: id
  }
  downloadFile(params).then(res => {
    if (res.code == 200) {
      window.open(res.data.url, '_self')
    }
  }).catch(e => {
    console.log(e)
  })

}

// 项目类型
export function getProjectTypeUtil (val) {
  let arr = []
  if (val != null && val.length > 0) {
    arr = JSON.parse(val)
  }
  return arr
}